<?php 
namespace Admin\Controller;

use Think\Controller;

//公共的控制器类
class BannerController extends AdminController{

    public function index()
    {
        $articles = M('experience')->field('id,title')->select();
        $this->assign('articles',$articles);
        $this->display();
    }
    public function upload()
    {
        if (empty($_POST)) {
            $this->error('非法操作!');
        }elseif (($_FILES[$_POST['bannerName']]['error']==4)) {
            $this->error('请选择一张图片好吗?');
            exit;
        }else{

            $banner_info = D('banner');
            if (!$banner_info->create()) {
                $this->error($banner_info->getError());
            }
            // var_dump($_POST);
            $upload = new \Think\Upload();// 实例化上传类
            $upload->maxSize = 3145728 ;// 设置附件上传大小
            $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
            $upload->rootPath = './Public/Uploads/'; // 设置附件上传根目录
            $upload->savePath = ''; // 设置附件上传（子）目录
            $upload->saveName = $_POST['bannerName'];
            $upload->autoSub = false;// 不使用子目录
            $upload->replace = true;// 用新的banner覆盖旧的banner
            $_POST['url'] = U('Home/Content/index').'?id='.$_POST['url'];
            

            // 上传文件
            $info = $upload->upload();
            
            $path = array();
            $b_id = substr($upload->saveName, -1);
            $path['path']= '/Public/Uploads/'.$info[$upload->saveName]['savename'];
            // 图片处理
            $image = new \Think\Image();
            $image->open('.'.$path['path']);
            // 生成一个固定大小为730*271的缩略图并保存为bannerX.jpg|png....
            $image->thumb(730, 271,\Think\Image::IMAGE_THUMB_FIXED)->save('.'.$path['path']);
            $_POST['path'] = $path['path'];

            if(!$info) {// 上传错误提示错误信息
                $this->error($upload->getError());
                exit;
            }else{
                $banner_info->where('id = '.$b_id)->save($_POST);
                //如果数据库数据更新成功
                $this->success('上传成功！');
            }
        }
        
    }
}
 